# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=ppp
pkgver=2.4.9
pkgrel=4
pkgdesc="A daemon which implements the PPP protocol for dial-up networking"
url="https://ppp.samba.org/"
arch="all"
license="BSD-3-Clause AND BSD-4-Clause AND BSD-4-Clause-Shortened AND GPL-2.0-or-later AND HPND AND LGPL-2.0-or-later AND Public-Domain AND RSA-MD"
makedepends="bsd-compat-headers linux-headers openssl1.1-compat-dev libpcap-dev"
subpackages="$pkgname-doc $pkgname-dev $pkgname-chat $pkgname-radius
	    $pkgname-atm $pkgname-pppoe $pkgname-l2tp $pkgname-winbind
	    $pkgname-passprompt $pkgname-passwordfd $pkgname-minconn
	    $pkgname-openrc $pkgname-daemon
	    "

# for compatibility reasons the 'ppp' package is an empty meta package
# that pulls in all except -dev and -doc subpackages
depends="$pkgname-chat $pkgname-radius
	    $pkgname-atm $pkgname-pppoe $pkgname-l2tp $pkgname-winbind
	    $pkgname-passprompt $pkgname-passwordfd $pkgname-minconn
	    $pkgname-daemon
	    "

source="https://github.com/paulusmack/ppp/archive/ppp-$pkgver.tar.gz
	musl-fix-headers.patch
	fix-paths.patch
	0011-build-sys-don-t-put-connect-errors-log-to-etc-ppp.patch
	fix-pppd-magic.h.patch
	ip-down
	ip-up
	pppd.initd
	fix-linux-5.15-include.patch
	"

builddir="$srcdir"/$pkgname-$pkgname-$pkgver

# secfixes:
#   2.4.8-r1:
#     - CVE-2020-8597

build() {

	# fix CFLAGS
	# -D_GNU_SOURCE is needed for IPv6 to work apparently
	export CFLAGS="$CFLAGS -D_GNU_SOURCE"
	sed -i "s:-O2 -pipe -Wall -g:${CFLAGS}:" pppd/Makefile.linux
	sed -i "s:-g -O2:${CFLAGS}:" pppd/plugins/Makefile.linux
	sed -i "s:-O2:${CFLAGS}:" pppstats/Makefile.linux
	sed -i "s:-O2 -g -pipe:${CFLAGS}:" chat/Makefile.linux
	sed -i "s:-O:${CFLAGS}:" pppdump/Makefile.linux
	# enable active filter
	sed -i "s:^#FILTER=y:FILTER=y:" pppd/Makefile.linux
	# enable ipv6 support
	sed -i "s:^#HAVE_INET6=y:HAVE_INET6=y:" pppd/Makefile.linux
	# Enable Microsoft proprietary Callback Control Protocol
	sed -i "s:^#CBCP=y:CBCP=y:" pppd/Makefile.linux
	sed -i "s:^#CBCP=y:CBCP=y:" pppd/Makefile.linux
	sed -i "s:^#USE_CRYPT=y:USE_CRYPT=y:" pppd/Makefile.linux
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--localstatedir=/var
	make COPTS="$CFLAGS"
}

package() {
	make INSTROOT="$pkgdir" install
	install -Dm644 include/net/ppp_defs.h \
		"$pkgdir"/usr/include/net/ppp_defs.h
	install -D -m755 "$srcdir"/ip-up "$pkgdir"/etc/ppp/ip-up
	install -D -m755 "$srcdir"/ip-down "$pkgdir"/etc/ppp/ip-down
	install -D -m755 "$srcdir"/pppd.initd "$pkgdir"/etc/init.d/pppd
	install -D -m644 etc.ppp/options "$pkgdir"/etc/ppp/options
	install -D -m600 etc.ppp/pap-secrets "$pkgdir"/etc/ppp/pap-secrets
	install -D -m600 etc.ppp/chap-secrets "$pkgdir"/etc/ppp/chap-secrets

	# busybox ifup/ifdown needs pon/poff
	install -D -m644 scripts/pon.1 "$pkgdir"/usr/share/man/man1/pon.1
	install -D -m755 scripts/pon "$pkgdir"/usr/bin/pon
	install -D -m755 scripts/poff "$pkgdir"/usr/bin/poff

	install -d "$pkgdir"/usr/share/doc/ppp
	for i in scripts/*; do
		case $i in
		pon|poff|*.1) continue;
		esac
		if [ -f "$i" ]; then
			cp $i "$pkgdir"/usr/share/doc/ppp/
		fi
	done
	install -d "$pkgdir"/etc/ppp/peers
}

atm() {
	pkgdesc="pppd plugin to implement PPPoATM protocol"
	depends="ppp-daemon"
	license="GPL-2.0-or-later AND LGPL-2.0-or-later"
	_mv usr/lib/pppd/$pkgver/pppoatm.so 755
}

minconn() {
	pkgdesc="pppd plugin to implement a 'minconnect' option"
	depends="ppp-daemon"
	license="BSD-4-Clause"
	_mv usr/lib/pppd/$pkgver/minconn.so 755
}

winbind() {
	pkgdesc="WINBIND plugin for pppd"
	depends="ppp-daemon"
	license="GPL-2.0-or-later"
	_mv usr/lib/pppd/$pkgver/winbind.so 755
}


passprompt() {
	pkgdesc="pppd plugin to invoke an external PAP password prompter"
	depends="ppp-daemon"
	license="GPL-2.0-or-later"
	_mv usr/lib/pppd/$pkgver/passprompt.so 755
}

passwordfd() {
	pkgdesc="pppd plugin let's you pass the password via a file descriptor"
	depends="ppp-daemon"
	_mv usr/lib/pppd/$pkgver/passwordfd.so 755
}

chat() {
	pkgdesc="a program for automatic ppp session establishment"
	depends=
	license="Public-Domain"
	_mv usr/sbin/chat 755
}

l2tp() {
	pkgdesc="pppd plugin to implement PPPoL2TP protocol"
	depends="ppp-daemon"
	license="GPL-2.0-or-later"
	_mv usr/lib/pppd/$pkgver/openl2tp.so 755
	_mv usr/lib/pppd/$pkgver/pppol2tp.so 755
}

pppoe() {
	pkgdesc="pppd plugin for kernel-mode PPPoE on Linux"
	depends="ppp-daemon"
	license="GPL-2.0-or-later"
	_mv usr/lib/pppd/$pkgver/rp-pppoe.so 755
	_mv usr/sbin/pppoe-discovery 755
}

radius() {
	pkgdesc="RADIUS plugin for pppd"
	depends="ppp-daemon"
	license="GPL-2.0-or-later AND HPND"
	_mv usr/lib/pppd/$pkgver/radius.so 755
	_mv usr/lib/pppd/$pkgver/radattr.so 755
	_mv usr/lib/pppd/$pkgver/radrealms.so 755
	install -d "$subpkgdir"/etc/radiusclient
	cp -r "$builddir"/pppd/plugins/radius/etc/* "$subpkgdir"/etc/radiusclient
	mv "$subpkgdir"/etc/radiusclient/radiusclient.conf.in "$subpkgdir"/etc/radiusclient/radiusclient.conf
	sed -i "s:@pkgsysconfdir@:/etc/radiusclient:g" "$subpkgdir"/etc/radiusclient/radiusclient.conf
	sed -i "s:@sbindir@:/usr/sbin:g" "$subpkgdir"/etc/radiusclient/radiusclient.conf
}

_mv() {
	install -m $2 -D "$pkgdir"/$1 "$subpkgdir"/$1
	rm "$pkgdir"/$1
}

daemon() {
	pkgdesc="A daemon which implements the PPP protocol"
	depends=
	license="BSD-3-Clause AND BSD-4-Clause AND BSD-4-Clause-Shortened AND GPL-2.0-or-later AND LGPL-2.0-or-later AND Public-Domain AND RSA-MD"
	mkdir -p "$subpkgdir"
	mv "$pkgdir"/* "$subpkgdir"/
}

sha512sums="
c309f8f69f534c05547cd2f66dade0e0f198ea4c2928a7e899e660280786b3e965437a67b8c5bb81c59d0fa1818b4eb7b701d2dce015a420d380422d2bca4e1a  ppp-2.4.9.tar.gz
29fe71a64d1370aa9a939767261a5d3b69c35e56e2fba0598477cf4986ad6dc4aff3dfa12562e8acf38272bad5b3773c8a5104599da872f7dd995bf2e64bfb58  musl-fix-headers.patch
7b84e2085c5e399a8f9e0cb928a7b4d5077f42755efa8961c848e287a8b8b78f3a123b59fbac76610ce8fe1f81fa103188b2e3ad4ffd82d14cb82243e2edeb91  fix-paths.patch
b490971d03fef4de66b61123f80a0087270bcb88466ae8ed98ea9a08b35d4c7c46b2dadd304e2970a4206bb5760a14370d7e3873de6240119d88e927ecef840c  0011-build-sys-don-t-put-connect-errors-log-to-etc-ppp.patch
d1067defff79d6c9f67121a9214e41a1bcca1e3b8a345ad905d223fdb8835142bad7cc3b556a3eca509ddf51cf808741773f31f4dca74e834b612a15854a5e6b  fix-pppd-magic.h.patch
160260bd2a788beaac395eadb38efbfd1e0e1a458fe151555acacf2c4f3a12381d644672c664f9793db6e3459a4f05a54e3ff6d407b0d37e8014b15bd0f11bcc  ip-down
8258c95a6b6e8e94d6001b5cd3a99bd3270f29ba0f2e5050a26c8d5f1b67eead8d039e71ce86a784d45e620047b0a2bb14b258d80a9ea62084ba588a4c2e2d84  ip-up
bd6f43588b037367ffdb57f5e331492dcaa5969003e219c2dc8b90e6be1aa407282ff6114b91d1379ebeff766983fa0622456520cc0ac592b4f0b1496acf21bf  pppd.initd
c8e8a05c1857ae98aa712683d7af9cd2df5a229aed4ef1f55e430cbb5468db93103b5a772191d33d063882525769fca667d0ac3a6233fdd42e61a58ead896b6c  fix-linux-5.15-include.patch
"
